import { NormalColony } from './NormalColony';

export class RoleCounter {

    readonly colony: NormalColony;

    constructor(colony: NormalColony) {
        this.colony = colony;
    }

    /**
     * 一切存活的爬的数量
     */
    countAlive(role: CreepRole) {
        return this.getAliveCreeps(role).length;
    }
    getAliveCreeps(role: CreepRole, predicate = (creep: Creep) => true) {
        return this.colony.getCreepsByRole(role).filter(predicate);
    }

    /**
     * 已计划但尚未孵化的爬的数量
     */
    countInSpawn(role: CreepRole) {
        return this.getSpawningCreeps(role).length;
    }
    getSpawningCreeps(role: CreepRole, predicate = (req: SpawnRequest) => true) {
        return (this.colony.memory.spawnList || []).filter(r => r.role === role && predicate(r));
    }

    /**
     * 存活的数量与计划而尚未孵化的数量之总和
     */
    count(role: CreepRole) {
        return this.countAlive(role) + this.countInSpawn(role);
    }

    /**
     * 有充足的 TTL 的爬的数量
     */
    countHealthy(role: CreepRole) {
        return this.getHealthyCreeps(role).length;
    }
    getHealthyCreeps(role: CreepRole, predicate = (creep: Creep) => true) {
        return this.colony.getCreepsByRole(role).filter(creep => {
            return creep.ticksToLive >= creep.body.length * CREEP_SPAWN_TIME && predicate(creep);
        });
    }

    /**
     * 一切可用的（没有老年期）爬的数量
     */
    countUsable(role: CreepRole) {
        return this.countHealthy(role) + this.countInSpawn(role);
    }

}